<?php
namespace app\shell;


use share\console\CConsole;
use share\console\CLIRunner;
use share\utils\Helper;

class CServiceGenerate extends CLIRunner{

    public function generate_action($table,$controllerModule = 'bossManager'){
        $className = ucfirst(Helper::underscoreToCamel($table));
        $templateClassName = 'Template';
        $paths = [
            'controller' => APP_PATH."controller".DIRECTORY_SEPARATOR.$controllerModule.DIRECTORY_SEPARATOR."{{class}}Controller.php",
            'validate' => APP_PATH."validate".DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR."{{class}}Validate.php",
            'service' => APP_PATH."service".DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR."{{class}}Service.php",
            'model' => APP_PATH."model".DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR."{{class}}Model.php",
        ];

        foreach($paths as $path){
            $toPath = str_replace("{{class}}",$className,$path);
            if(file_exists($toPath)){
                $override = CConsole::confirm("file ".$toPath." already exists! override ?",false);
                if(!$override){
                    continue;
                }
            }
            $fromPath = str_replace("{{class}}",$templateClassName,$path);
            if(!file_exists($fromPath)){
                CConsole::print_line("template file of ".$fromPath." is not exists. ignored!");
                continue;
            }
            $content = file_get_contents($fromPath);
            $content = str_replace($templateClassName,$className,$content);
            file_put_contents($toPath,$content);
        }

    }
}